package com.kqb.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kqb.common.domain.page.PageResult;
import com.kqb.common.utils.Assert;
import com.kqb.common.utils.DateUtils;
import com.kqb.mapper.ForwarderInquiryListMapper;
import com.kqb.mapper.ForwarderInquiryMapper;
import com.kqb.mapper.ForwarderInquiryProductMapper;
import com.kqb.pojo.ForwarderInquiryList;
import com.kqb.pojo.ForwarderInquiryProduct;
import com.kqb.query.ForwarderInquiryQuery;
import com.kqb.service.ForwarderInquiryListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ForwarderInquiryListServiceImpl implements ForwarderInquiryListService {

    String maxCode; //   最大编码

    @Autowired
    ForwarderInquiryListMapper inquiryListMapper;
    @Autowired
    ForwarderInquiryProductMapper inquiryProductMapper;
    @Autowired
    ForwarderInquiryMapper inquiryMapper;

    @Transactional
    @Override
    public int save(ForwarderInquiryList entity) {
        //保存询价单
        int i1 = inquiryMapper.save(generateNewCode());
        Assert.isZero(i1,"保存询价单失败");
        //询价编码添加到询价详情中
        entity.setInquiryCode(maxCode);
        //判断询价询价单是否添加成功
        int i = inquiryListMapper.save(entity);
        Assert.isZero(i,"保存货代询价详情单失败！");
        List<ForwarderInquiryProduct> products = entity.getInquiryProducts();
        if (products != null) {
            for (ForwarderInquiryProduct product : products) {
                product.setInquiryCode(maxCode);
                int saved = inquiryProductMapper.saveInquiryProduct(product);
                Assert.isZero(saved,"保存货代询价产品失败！");
            }
        }
        /*
        * 后面还要写保存报价信息(根据货代人擅长的运输方式和路线)*/

        return 1;
    }


    /**
     * 生成新的编码
     * @return
     */
    String generateNewCode() {
        //获得当前时间的年月 如2502
        String ym = DateUtils.getSysDateYM();
        //判断最大编码是否存在
        if (null == maxCode) {
            //如果不存在，则查询数据库
            maxCode = inquiryListMapper.queryMaxCode();
            //如果当前没有编码 说明当前的询价格是当月的第一笔
            if (null == maxCode) {
                //产生新的编码
                maxCode = "HDXJ" + ym + "0001";
                return maxCode;
            }
        }
        //执行到这这里 获取最大编码 HDXJ25020001  计算新的编码
        String temYM = maxCode.substring(4,8);
        if (!ym.equals(temYM)) {
            //说明当前最大编码不是当月的编码
            maxCode= "HDXJ" + ym +"0001";
            return maxCode;
        }
        //如果到这里 是当月的最大编码 序号累加XJ25020002
        String prefix = maxCode.substring(0,8);
        String seq = maxCode.substring(8);
        int num = Integer.parseInt(seq);
        num++;
        maxCode = prefix + String.format("%04d",num);
        return maxCode;
    }

    /**
     * 查询货代询价
     * @param map
     * @return
     */
    @Override
    public PageResult queryByMap(Map map) {
        int page=1;//当前第几页
        int pageSize=10;//默认每页记录数
        if(map.containsKey("pageNum")){
            page=Integer.parseInt(map.get("pageNum").toString());
        }
        if(map.containsKey("pageSize")){
            pageSize=Integer.parseInt(map.get("pageSize").toString());
        }
        PageHelper.startPage(page,pageSize);
        List list=inquiryListMapper.queryByMap(map);
        PageInfo info=new PageInfo(list);
        return new PageResult(page,pageSize,info.getTotal(),list);
    }

    @Override
    public ForwarderInquiryList queryByCode(String code) {
        return inquiryListMapper.queryByCode(code);
    }


    @Override
    public int updateInquiry(ForwarderInquiryList entity) {
        int i = inquiryListMapper.updateInquiry(entity);
        Assert.isZero(i,"更新询价详情失败！");
        return 1;
    }
}
